//
//  YTFeedBackViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/11.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTFeedBackViewController.h"
#import "YTFeedBackViewModel.h"

@interface YTFeedBackViewController ()
@property (weak, nonatomic) IBOutlet UITextView *feedText;
@property (weak, nonatomic) IBOutlet UIButton *submitFeedBtn;
@property (weak, nonatomic) IBOutlet UIButton *inlandPhoneBtn;
@property (weak, nonatomic) IBOutlet UIButton *internationalPhoneBtn;
@property (weak, nonatomic) IBOutlet UILabel *placeHolder;
@property (weak, nonatomic) IBOutlet UIButton *visitOrgBtn;

@property (strong, nonatomic) YTFeedBackViewModel *viewModel;
@end

@implementation YTFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initialize {
    _viewModel = [[YTFeedBackViewModel alloc] init];
}

- (void)bindViewModel {
    RAC(_viewModel, feedBackContent) = _feedText.rac_textSignal;
    _submitFeedBtn.rac_command = _viewModel.submitCommand;
    
    @weakify(self);
    [[_viewModel.submitCommand.executionSignals switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        [SVProgressHUD showSuccessWithStatus:@"感谢您的反馈\n我们会尽快回复哒"];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"提交失败"];
    }];
    
    [_viewModel.feedBackContentSignal subscribeNext:^(NSNumber *enabled) {
        @strongify(self);
        self.placeHolder.hidden = enabled.boolValue;
        self.submitFeedBtn.backgroundColor = enabled.boolValue ? kMainBlueColor : [UIColor lightGrayColor];
    }];
    
    [[_inlandPhoneBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://4000630655"]];
    }];
    
    [[_internationalPhoneBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://8657128297302"]];
    }];
    
    [[_visitOrgBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.yitu8.cn"]];
    }];
}
@end
